package com.xydtech.ecard.config.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.constant.UserConstants;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.ecard.config.domain.EcardBusiReportConfig;
import com.xydtech.ecard.config.service.IEcardBusiReportConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电子名片业务填报类型配置Controller
 *
 * @author yuzhiguo
 * @date 2024-09-25 上午10:03
 */
@Controller
@RequestMapping("/ecard/config/busiReportConfig")
public class EcardBusiReportConfigController extends BaseController
{
    private String prefix = "ecard/config/busiReportConfig";

    @Autowired
    private IEcardBusiReportConfigService ecardBusiReportConfigService;

    @RequiresPermissions("ecard:config:busiReportConfig:view")
    @GetMapping()
    public String config()
    {
        return prefix + "/busiReportConfig";
    }

    /**
     * 查询电子名片业务填报类型配置列表
     */
    @RequiresPermissions("ecard:config:busiReportConfig:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EcardBusiReportConfig ecardBusiReportConfig)
    {
        startPage();
        List<EcardBusiReportConfig> list = ecardBusiReportConfigService.selectEcardBusiReportConfigList(ecardBusiReportConfig);
        return getDataTable(list);
    }

    /**
     * 导出电子名片业务填报类型配置列表
     */
    @RequiresPermissions("ecard:config:busiReportConfig:export")
    @Log(title = "电子名片业务填报类型配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EcardBusiReportConfig ecardBusiReportConfig)
    {
        List<EcardBusiReportConfig> list = ecardBusiReportConfigService.selectEcardBusiReportConfigList(ecardBusiReportConfig);
        ExcelUtil<EcardBusiReportConfig> util = new ExcelUtil<EcardBusiReportConfig>(EcardBusiReportConfig.class);
        return util.exportExcel(list, "电子名片业务填报类型配置数据");
    }

    /**
     * 新增电子名片业务填报类型配置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存电子名片业务填报类型配置
     */
    @RequiresPermissions("ecard:config:busiReportConfig:add")
    @Log(title = "电子名片业务填报类型配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EcardBusiReportConfig ecardBusiReportConfig)
    {
        ecardBusiReportConfig.setCreatorId(ShiroUtils.getSysUser().getUserId());
        if (UserConstants.CONFIG_KEY_NOT_UNIQUE.equals(ecardBusiReportConfigService.checkBusiCodeUnique(ecardBusiReportConfig)))
        {
            return error("新增业务'" + ecardBusiReportConfig.getBusiName() + "'失败，业务唯一标识键名已存在");
        }
        return toAjax(ecardBusiReportConfigService.insertEcardBusiReportConfig(ecardBusiReportConfig));
    }

    /**
     * 修改电子名片业务填报类型配置
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        EcardBusiReportConfig ecardBusiReportConfig = ecardBusiReportConfigService.selectEcardBusiReportConfigById(id);
        mmap.put("ecardBusiReportConfig", ecardBusiReportConfig);
        return prefix + "/edit";
    }

    /**
     * 修改保存电子名片业务填报类型配置
     */
    @RequiresPermissions("ecard:config:busiReportConfig:edit")
    @Log(title = "电子名片业务填报类型配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EcardBusiReportConfig ecardBusiReportConfig)
    {
        if (UserConstants.CONFIG_KEY_NOT_UNIQUE.equals(ecardBusiReportConfigService.checkBusiCodeUnique(ecardBusiReportConfig)))
        {
            return error("修改业务'" + ecardBusiReportConfig.getBusiName() + "'失败，业务唯一标识键名已存在");
        }
        return toAjax(ecardBusiReportConfigService.updateEcardBusiReportConfig(ecardBusiReportConfig));
    }

    /**
     * 删除电子名片业务填报类型配置
     */
    @RequiresPermissions("ecard:config:busiReportConfig:remove")
    @Log(title = "电子名片业务填报类型配置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ecardBusiReportConfigService.deleteEcardBusiReportConfigByIds(ids));
    }
}
